package com.ycsj.goldmedalnewconcept.common.activity;

import android.content.Context;
import android.content.Intent;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.utils.StateBarUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

/**
 * 错题集界面
 * Created by Kevin on 2019/5/16
 */
public class ErrorQuestionListActivity extends BaseActivity {

    private WebView webview;

    private String url;

    private String lessonid;

    private String classnum;

    private String moduleType;


    @Override
    protected int getContentLayout() {

        return R.layout.activity_errorlist;
    }

    /**
     * 跳转到当前页面的方法
     *
     * @param context
     * @param lessonid
     */
    public static void goActivity(Context context, String lessonid, String classNum, String moduleType) {
        Intent it = new Intent(context, ErrorQuestionListActivity.class);
        it.putExtra("lessonid", lessonid);
        it.putExtra("classnum", classNum);
        it.putExtra("moduleType", moduleType);
        context.startActivity(it);
    }

    @Override
    protected void init() {

        //沉浸状态栏
        StateBarUtil.customStateBar(this, getResources().getColor(R.color.h5title));

        initView();

        initIntent();

        initData();
    }

    /**
     * 接收数据
     */
    private void initIntent() {

        lessonid = getIntent().getStringExtra("lessonid");
        classnum = getIntent().getStringExtra("classnum");
        moduleType = getIntent().getStringExtra("moduleType");

        //错题集
        //教师
        if ("2".equals(AccountUtils.getRole(this))) {
            url = HttpAddress.ERRORQUESTIONLIST + "?tel=" +
                    AccountUtils.getAccount(this) + "&role=" + AccountUtils.getRole(this)
                    + "&classnum=" + classnum + "&lessonid=" + lessonid + "&moduleType=" + moduleType + "&schoolid=" +
                    AccountUtils.getSchoolId(this);

        } else {
            //学生
            url = HttpAddress.ERRORQUESTIONLIST + "?tel=" +
                    AccountUtils.getAccount(this) + "&role=" + AccountUtils.getRole(this)
                    + "&lessonid=" + lessonid + "&classnum=" + AccountUtils.getClassNum(this) + "&moduleType=" + moduleType
                    + "&schoolid=" + AccountUtils.getSchoolId(this) + "&teacherid=" +
                    AccountUtils.getTeacher(this);
        }
    }

    /**
     * 初始化控件
     */
    private void initView() {
        webview = findViewById(R.id.webView);
    }

    /**
     * 初始化数据
     */
    private void initData() {

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBuiltInZoomControls(false);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setDatabaseEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webview.getSettings().setPluginState(PluginState.ON);
        webview.loadData("", "text/html", null);

        webview.getSettings().setAllowFileAccess(true);
        webview.addJavascriptInterface(new ErrorQuestionListActivity.WebAppInterface(this), "Android");
        webview.setWebChromeClient(new WebChromeClient());
        webview.loadUrl(url);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }


    public class WebAppInterface {
        Context mContext;

        WebAppInterface(Context c) {
            mContext = c;
        }

        // 关闭界面
        @JavascriptInterface
        public void finish() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ErrorQuestionListActivity.this.finish();
                }
            });

        }
    }


    @Override
    public void onPause() {
        super.onPause();
        webview.onPause();
        webview.pauseTimers();
    }

    @Override
    public void onResume() {
        super.onResume();
        webview.resumeTimers();
        webview.onResume();
    }

    @Override
    protected void onDestroy() {
        webview.removeJavascriptInterface("Android");
        webview.destroy();
        webview = null;
        super.onDestroy();
    }

}
